<?php

class StatisticController extends Zend_Controller_Action 
{
    function init()
    {
        Zend_Loader::loadClass('Rights');
        Zend_Loader::loadClass('Attendance');
        Zend_Loader::loadClass('Invoice');
        Zend_Loader::loadClass('Studentpayment');
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Lesson');
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
        
        $user = new User();
        $this->view->teacher_ids = $user->fetchAll('authgroup_id=2','last_name');
        $this->view->student_ids = $user->fetchAll('authgroup_id=1','last_name');
        
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }
    
    function indexAction()
    {
        $this->view->title = "Статистика";
        $this->view->content_title = "Статистика";
        
    }
    
    function teacherAction()
    {
        
        $this->view->item = new stdClass();
        
        $this->view->item->from_date = $this->_request->getParam('from_date', 0)?$this->_request->getParam('from_date', 0):date('Y-m-01');
        $this->view->item->to_date = $this->_request->getParam('to_date', 0)?$this->_request->getParam('to_date', 0):date('Y-m-t');
        $this->view->item->teacher_id = $this->_request->getParam('teacher_id', 0)?$this->_request->getParam('teacher_id', 0):0;
        
        $l = new Lesson();
        $return = $l->getTeacherLessons($this->view->item->teacher_id,$this->view->item->from_date,$this->view->item->to_date);
        
        $this->view->quantity = $return['count'];
        $this->view->hours = $return['hours'];
    }
    
    function studentAction()
    {
        $this->view->item = new stdClass();
        
        $this->view->item->from_date = $this->_request->getParam('from_date', 0)?$this->_request->getParam('from_date', 0):date('Y-m-01');
        $this->view->item->to_date = $this->_request->getParam('to_date', 0)?$this->_request->getParam('to_date', 0):date('Y-m-t');
        $this->view->item->student_id = $this->_request->getParam('student_id', 0)?$this->_request->getParam('student_id', 0):0;
        
        $l = new Attendance();
        $return = $l->getUserLessons($this->view->item->student_id,$this->view->item->from_date,$this->view->item->to_date);
        $this->view->quantity = $return;
        
        $p = new Studentpayment();
        $this->view->payment = $p->getLastPaymant($this->view->item->student_id);
        
        $inv = new Invoice();
        $this->view->end_date = $inv->getLastInvoice($this->view->item->student_id,$this->view->item->from_date,$this->view->item->to_date);
        
    }
    
    function globalAction()
    {
        
    }
    
}